package acs.bridge.jreport;

import acs.bridge.dao.RawTableMailDAOImpl;
import acs.bridge.model.RawTableMail;

import javax.mail.MessagingException;
import javax.mail.internet.InternetAddress;
import java.io.IOException;
import java.util.Collection;

import org.springframework.mail.SimpleMailMessage;

/**
 * acs.jreport
 * User: KuznetsovStV
 * Date: 07.04.13
 * Time: 15:20
 */
public class JReportSaveMailDataBase extends JReportFileUse implements JReportSaveInterface {


    private RawTableMailDAOImpl rawTableDAOImpl;
  //  private JavaMailSenderImpl mailSender;
    private JReportMailMessage templateMessage;

    public void saveReport(byte[] data, String extension) throws IOException, MessagingException {

    }

    public void setReportName(String reportName) {
        fileName = reportName;
    }

    public String getReportName() {
        return fileName;
    }


    public void saveReportItem(Collection<JReportItem> jReportItemCollection) throws Exception {
        byte[] outdata = archiveReport(jReportItemCollection);

        //fullFileName = getFullFileName((utilityArcInterface.getExtension() == null ? extension : utilityArcInterface.getExtension()));
        String fullFileName = getFullFileName(utilityArcInterface.getExtension());

        templateMessage.setTemplate(getParameters());

        RawTableMail rawTableMail = new RawTableMail();


        rawTableMail.setSend_adr(templateMessage.getFrom());

        String l_adr_str = "";
        if (templateMessage.getTo() != null) {

            String l_ch = "";
            for (int i = 0; i < templateMessage.getTo().length; i++) {
                InternetAddress[] l_adr = InternetAddress.parse(templateMessage.getTo()[i], true);
                for (int p = 0; p < l_adr.length; p++) {
                    if (l_adr[p] != null)
                    {
                        l_adr_str = l_adr_str + l_ch + l_adr[p];
                         if (l_ch == "") l_ch = ",";
                    }
                }
            }
            rawTableMail.setRecip_adr(l_adr_str);
        }

        if (templateMessage.getSubject() != null) {
            String l_subject = String.format(templateMessage.getSubject(), getReportName());
            rawTableMail.setMail_subject(l_subject);
        }


        /*rawTableMail.setSmtp_host(mailSender.getHost());
        rawTableMail.setSmtp_user(mailSender.getUsername());
        rawTableMail.setSmtp_pass(mailSender.getPassword());    */
        rawTableMail.setAttach_data(outdata);
        rawTableMail.setArch_enable("0");

        rawTableMail.setAttach_name(fullFileName);
        rawTableMail.setMail_body(templateMessage.getText());

        logger.info("Save e-mail to database {filename=" + fullFileName + ",subject=" + templateMessage.getSubject() + ",to=" + l_adr_str + "}");

        getRawTableDAOImpl().Insert(rawTableMail);


    }

    public void setRawTableDAOImpl(RawTableMailDAOImpl rawTableDAOImpl) {
        this.rawTableDAOImpl = rawTableDAOImpl;
    }

    public RawTableMailDAOImpl getRawTableDAOImpl() {
        return rawTableDAOImpl;
    }

   /* public void setMailSender(JavaMailSenderImpl mailSender) {
        this.mailSender = mailSender;
    }

    public JavaMailSenderImpl getMailSender() {
        return mailSender;
    }   */

    public void setTemplateMessage(SimpleMailMessage templateMessage) {
        this.templateMessage = (JReportMailMessage) templateMessage;
    }

    public SimpleMailMessage getTemplateMessage() {
        return templateMessage;
    }
}
